package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.elmspringboot.controller.utils.UserDTO;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.User;
import com.example.elmspringboot.security.Jwt.JwtToken;
import com.example.elmspringboot.security.Jwt.JwtUtil;
import com.example.elmspringboot.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Tag(name = "UserController", description = "用户操作界面")
@RestController
//@Controller
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;
    @Operation(summary = "用户登录")
    @PostMapping(path = "/login")
    public Result login(String username, String password) {
        if(username==null || password==null){
            return new Result(false,null,"没有参数");
        }
        // 返回Jwt
        String usernamePasswordToken = JwtUtil.sign(username,password);
        // 通过名字返回user信息
        User userInfo = userService.getUserByName(username);
        // 移交securityManager,getSubject()返回subject，然后subject.login()移交给JwtShiroRealm但是会被JwtFilter拦截
        SecurityUtils.getSubject().login(new JwtToken(usernamePasswordToken));

        return new Result(true, new UserDTO(usernamePasswordToken,userInfo),"登录成功");
    }
    @GetMapping("/{id}")
    public Result getUser(@PathVariable Integer id){
        return new Result(true,userService.getById(id));
    }
    @Operation(summary = "用户注册")
    @PostMapping(path = "/register",consumes = "multipart/form-data")
    public Result register(String username, String password) {
        System.out.println(username);
        System.out.println(password);
        return new Result(true, userService.register(username, password),"success");
    }
    @PostMapping("/update")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam Integer userId) {
        if (file.isEmpty()) {
            return new Result(false,null,"图片不能为空");
        }
        String filePath = "D:/Java2022Soft/elm_vue_webpack/public/images/user/"+file.getOriginalFilename();
        System.out.println(filePath);
        File saveDir = new File(filePath);
        if (!saveDir.getParentFile().exists())
            saveDir.getParentFile().mkdirs();
        try {
            file.transferTo(saveDir);
            User user = userService.getById(userId);
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("user_id",user.getId());
            updateWrapper.set("user_img","/images/user/" + file.getOriginalFilename());
            userService.update(user,updateWrapper);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new Result(true," /images/user/" + file.getOriginalFilename(),"成功了");
    }

//    @GetMapping("/login")
//    public String login(){
//        return "login";
//    }
//    @GetMapping("/userLogin")
//    public String userLogin(String name, String password, HttpSession session){
//        // 1获取subject对象
//        Subject subject = SecurityUtils.getSubject();
//        // 2封装请求数据到token
//        AuthenticationToken token = new UsernamePasswordToken(name,password);
//        // 3调用login进行登录认证
//        try {
//            subject.login(token);
//            session.setAttribute("user", token.getPrincipal().toString());
//            return "main";
////            return "登录成功";
//        }catch (AuthenticationException e){
//            e.printStackTrace();
//            System.out.println("登录失败");
//            return  "登陆失败";
//        }
//    }
}

